<template>
  <el-dialog
    :model-value="show"
    :title="`${name}编辑`"
    width="800px"
    append-to-body
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    class="form-label-120"
    destroy-on-close
  >
    <div class="form-title">基础信息</div>
    <div class="form-box-readonly">
      <div class="form-item-readonly">
        <span class="form-label-readonly">{{ name }}ID：</span>
        <span class="form-value-readonly">
          {{ detailForm.id || DEFAULT_TEXT }}
        </span>
      </div>
      <div class="form-item-readonly">
        <span class="form-label-readonly">
          {{ dialogInfo?.dialogNameLabel || name + '名称' }}：
        </span>
        <span class="form-value-readonly">
          {{ detailForm.name || DEFAULT_TEXT }}
        </span>
      </div>
      <div class="form-item-readonly">
        <span class="form-label-readonly">更新人：</span>
        <span class="form-value-readonly">
          {{
            userStore().userMap[detailForm.updater_id]?.label || DEFAULT_TEXT
          }}
        </span>
      </div>
      <div class="form-item-readonly">
        <span class="form-label-readonly">更新时间：</span>
        <span class="form-value-readonly">
          {{ FORMAT_DATE(detailForm.updated_at) }}
        </span>
      </div>
    </div>
    <div class="form-title">权限设置</div>
    <RoleConfig
      @changeRole="changeRoleConfig"
      :visble_companies="detailForm.visible_companies"
    />
    <template #footer>
      <el-button
        type="primary"
        :loading="loading"
        :disabled="!show"
        @click="submitApply"
      >
        提交
      </el-button>
      <el-button type="info" @click="handleClose">取消</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { DEFAULT_TEXT, FORMAT_DATE } from '@/utils/const';
import RoleConfig from '@/components/RoleConfig';
import userStore from '@/store/modules/user';
import { platformSiteTypeCompanyUpdate } from '@/api/system';
const props = defineProps({
  show: {
    type: Boolean,
    default: false
  },
  activeRow: {
    type: Object,
    default: () => {}
  },
  name: {
    type: String,
    default: ''
  },
  dialogInfo: {
    type: Object,
    default: () => {}
  }
});
const emit = defineEmits();

// 关闭
function handleClose() {
  emit('update:show', false);
}

function changeRoleConfig({ visibleList }) {
  detailForm.value.company_ids = visibleList || [];
}

// 提交
const loading = ref(false);
function submitApply() {
  loading.value = true;
  platformSiteTypeCompanyUpdate({
    company_ids: detailForm.value.company_ids || [],
    id: detailForm.value.id
  })
    .then(() => {
      $msg.success(`${props.name}编辑成功`);
      handleClose();
      emit('getTable');
    })
    .finally(() => {
      loading.value = false;
    });
}

// 初始化表单
const detailForm = ref({});
watch(
  () => props.show,
  val => {
    if (!val) return;
    // 赋默认值
    detailForm.value = JSON.parse(JSON.stringify(props.activeRow));
    detailForm.value.company_ids = detailForm.value.visible_companies || [];
  }
);
</script>

<style lang="scss" scoped></style>
